import java.lang.reflect.Constructor;

public class AccessPersonConstructors {
    public static void main(String[] args) {
        try {
            //加载Person类
            Class clz=Class.forName("pojo.Person");
            //调用Person类的无参构造创建Person类实例
            Object obj=clz.newInstance();
            //调用Person实例的toString()方法,属性均为默认值
            System.out.println(obj);

            //获取Person的单参构造 :private pojo.Person(String)
            Constructor c2=clz.getDeclaredConstructor(String.class);
            c2.setAccessible(true);
            obj=c2.newInstance("new pojo.Person");
            System.out.println(obj);

            //获取Person的三参构造
            Constructor c3=clz.getDeclaredConstructor(String.class,String.class,String.class);
            c3.setAccessible(true);
            obj=c3.newInstance("new pojo.Person","beijing","Hello!");
            System.out.println(obj);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
